<template>
  <div class="total-box">
    <!-- 左侧树结构 -->
    <div class="head-container">
      <el-tree
        style="height: 100%"
        :data="deptOptions"
        :props="defaultProps"
        :expand-on-click-node="false"
        ref="tree"
        node-key="id"
        highlight-current
        @node-click="handleNodeClick"
        :default-expanded-keys="defaultExpandedKeys"
      >
      <template slot-scope="{node,data}">
          <span class="el-tree-node_box" :title="data.label">{{ data.label }}</span>
        </template>
      </el-tree>
    </div>

    <!-- 右侧 -->
    <div class="app-container">
      <el-row :gutter="20" style="height: 100%">
        <!--用户数据-->
        <el-col :span="24" :xs="24">
          <HkSearchform
            :data="queryParams"
            :field-list="filterInfo.fieldList"
            :list-type-info="dictList"
            @handleSearch="handleQuery"
            @handleReset="resetQuery"
            @handleEvent="handleEvent"
          />

          <el-row :gutter="10" style="margin-left:0;">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['system:user:add']">新建</el-button>
            <el-button
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['system:user:remove']"
            >删除</el-button>
            <el-button icon="el-icon-download" size="mini" @click="importTemplate">下载模板</el-button>
            <el-button @click="handleImport" size="mini" v-hasPermi="['system:user:import']">导入</el-button>
            <el-button @click="handleExport" size="mini">导出</el-button>
          </el-row>

          <hk-table
            ref="tableRef"
            :loading="loading"
            :tableData="tableData"
            :tableColumns="tableList"
            :showIndex="true"
            :total="total"
            :selection="true"
            :operate="true"
            :tableHeight="80"
            :query-params="queryParams"
            @handleSelectionChange="handleSelectionChange"
            @changePage="getListData"
          >
      
            <template #secLevel="scope">
              <dict-label :options="dictList.secLevelList" :value="scope.row.secLevel" />
            </template>
            <template #nationType="scope">
              <dict-label :options="dictList.nationTypeList" :value="scope.row.nationType" />
            </template>

            <template #operate="scope">
              <el-button size="mini" type="text" @click="handleUpdate(scope.row)" v-hasPermi="['system:user:edit']">修改</el-button>
              <el-button size="mini" type="text" @click="handleDelete(scope.row)" v-hasPermi="['system:user:remove']">删除</el-button>
            </template>
          </hk-table>
        </el-col>
      </el-row>
    </div>
    <!-- 新增和编辑的对话框 -->
    <EditDialog
      ref="editDialogRef"
      :title="title"
      @handleAdd="getList"
      :dictList="dictList"
      :treeId="treeId"
      :total="total"
    ></EditDialog>
    <!-- 输出 -->
    <Upload ref="import_dia" @submit="getList" :url="importUrl"></Upload>
  </div>
</template>

<script>
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import HkTable from '@/components/HKTable/index.vue'
import EditDialog from './component/editDialog.vue'
import { listDept,getDictsBatch, pageList, deleteBatch, dictList, treeselect } from '@/api/system/user.js'

import Upload from './component/Upload'
import HkSearchform from '@/components/HKSearchform/index.vue'
import DictLabel from '@/components/Common/dictLabel.vue'

const baseUrl = window.baseUrl.asset
export default {
  components: {
    Treeselect,
    HkTable,
    EditDialog,
    Upload,
    HkSearchform,
    DictLabel
  },
  data() {
    return {
      defaultExpandedKeys:['root'],
      treeId: '',
      // 搜索项配置
      filterInfo: {
        fieldList: [
          { label: '关键字', type: 'input', field: 'name', placeholder: '请输入用户名称' },
          {
            label: '涉密等级',
            type: 'select',
            field: 'secLevel',
            list: 'secLevelList'
          },
          {
            label: '在岗类别',
            type: 'select',
            field: 'nationType',
            list: 'nationTypeList'
          }
        ]
      },
      importUrl:window.baseUrl+'/system/user/import',
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,

      // 表格数据
      tableData: [{}],
      //  表格
      tableList: [
        { label: '人员名称', param: 'name' },
        { label: '人员编号', param: 'userNo'},
        { label: '所属部门', param: 'fullName' ,width:'340' },
        { label: '涉密等级', param: 'secLevel', type: 'slot' },
        { label: '在岗类别', param: 'nationType', type: 'slot' },
        { label: '岗位', param: 'post' }
      ],
      // 弹出层标题
      title: '',
      // 部门树选项
      deptOptions: undefined,
      // 树结构参数
      defaultProps: {
        children: 'children',
        label: 'label'
      },

      // 查询参数
      queryParams: {
        size: 10,
        current: 1
      },
      // 列表规则
      dictList: {
        foList: [
          {
            label: '是',
            value: '0'
          },
          {
            label: '否',
            value: '1'
          }
        ],

        userBusTypeList: [],
        secLevelList: [],
        nationTypeList: [],
        sys_user_sexList: [],
        politicalClimateList: [],
        nationList: [],
        attention: []
      }
    }
  },

  mounted() {
    this.getList()

    this.getDataList()
  },
  methods: {
    getDataList() {
      getDictsBatch('secLevel,userBusType,nationType,equipmentLocalized,sys_user_sex,politicalClimate,nation').then(res => {
       
        for (let key in res) {
          this.dictList[key + 'List'] = res[key].map(item => {
            return {
              value: item.dictVal,
              label: item.dictLabel
            }
          })
        }
      })
  
    },
    // 循环遍历
    orgLoop(val) {
      if (!val.length) return
      val.forEach(item => {
        item.value = item.id
        if (item.children) {
          this.orgLoop(item.children)
        }else{
          return
        }
      })
    },
    /** 查询用户列表 */
    getList(val) {
      treeselect().then(res => {
        this.dictList.orgList = res
        this.deptOptions = res
        this.defaultExpandedKeys=[res[0].id]
        this.orgLoop(this.dictList.orgList)
        this.orgLoop( this.deptOptions)
      })
      // listDept().then(res => {
       
      // })
      this.getListData(val)
    },
    getListData(val) {
      this.loading = true
      this.queryParams = { ...this.queryParams, ...val }

      pageList(this.queryParams ).then(res => {
        this.total = res.total
        this.tableData = res.list
        this.loading = false
      })
    },

    // 节点单击事件
    handleNodeClick(data,node) {

      this.queryParams.current = 1
      this.queryParams.size = 10
      this.treeId = data.id

      this.getListData({ orgId: data.id })
    },

    /** 搜索按钮操作 */
    handleQuery(val) {
      this.queryParams.current = 1
      this.getListData()
    },
    // 重置表单
    resetQuery() {
      for (const key in this.queryParams) {
        this.queryParams[key] = undefined
      }
      this.queryParams.current = 1
      this.queryParams.size = 10
      this.queryParams.orgId=''
      this.$refs.tree.setCurrentKey(null)
      this.getListData()
    },
    // 搜索框变化
    handleEvent(val) {
      let field = val.field
      let value = val.value
      this.queryParams = {
        ...this.queryParams,
        ...{
          [field]: value
        }
      }
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.title = '添加'
      this.$refs.editDialogRef.show()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.title = '编辑'

      this.$refs.editDialogRef.show(row.id)
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id || this.ids.join(',')
      this.$modal
        .confirm('是否确认删除所选数据项？')
        .then(function() {
          return deleteBatch(id)
        })
        .then(() => {
          this.$modal.msgSuccess('删除成功')
          this.getList()
        })
        .catch(error => {
          console.log(error)
        })
    },

    // 修改状态
    handleStatusChange(row) {},
    /** 导出按钮操作 */
    handleExport() {
      this.gDownload(
        window.baseUrl+ '/system/user/export',
        {
          ...this.queryParams
        },
        `用户信息.xlsx`
      )
    },
    /** 导入按钮操作 */
    handleImport() {
      this.$refs.import_dia.upload.open = true
    },
    /** 下载模板操作 */
    importTemplate() {
      this.gDownload(window.baseUrl+'/system/user/importTemplate', {}, `用户导入模板.xlsx`)
    }
  }
}
</script>
<style lang="scss" scoped>
.total-box {
  font-size: 14px;
  display: flex;
  width: 100%;
  height: 100%;
}
.head-container {
  overflow: auto;
  padding: 20px;
  height: 100%;
  width: 270px;
  border-right: 1px solid rgba(0, 0, 0, 0.08);
}
.app-container {
  flex: 1;
  width: calc(100% - 340px);
}
.el-col {
  height: 100%;
  display: flex;
  flex-direction: column;
  &::v-deep .targetTable {
    flex: 1;
    overflow: auto;
  }
}
::v-deep .el-row {
  display: flex;
  flex-wrap: wrap;
}
</style>
